home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: C++ beginner quesion on data member access.
- Date: Mon, 15 Apr 1996 13:19:38 -0400
- Organization: Datalytics, Inc
- Message-ID: <3172852A.7A76@datalytics.com>
- References: <4kgb76$r3s@HOPPER.ACM.ORG> <316E7140.C5D@platinum.com>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Michael Scott wrote:
- >
- > Ken Varn wrote:
- > >
- > > I have been struggling with the proper way to declare and access data
- > > members in my classes. I was once told that a class should not allow any
- > > data members to be public and that they should only be accessed via member
- > > functions. Why? I know the reasons for declaring private data, but what do
- > > I gain if all I am doing is providing a member function to get the private
- > > data or set the private data. i.e. why call a getData() function that
- > > basically just returns the private data member as opposed to just declaring
- > > the pviate data member as public.
- >
- > Classes encapsulate the behavior of an object. Data members
- > represent the "state" of an object. Data access functions
- > provide access to the internal state of an object while allowing
- > the implementation of that state to vary without necessarily
- > changing the interface to the object.
- >
- > Pragmatically speaking though, there are classes and
- > applications where the trade-offs between encapsulation overhead
- > and direct public member access favor the public data.
- >
- > Balancing the trade-offs is what makes programming an art rather
- > than a strict science.
-
- More specifically, exposing a dm as public means you can never
- change the data type without risking breaking code dependent
- upon it. You also make that dm available for class
- users to change without your class being able to
- control it.
-
- Consider, for example, a simple string class. If you expose the
- length dm, then you allow the class user to read and write it.
- What's more, if you decide that you want to add reference
- counting to your string class, the length actually comes from
- the representation. Your string class is just a handle to the
- shared representation. Thus, you would have to duplicate the
- length in your string class' dm just so those class users
- dependent upon a dm to get the length can still work.
-
- If, on the other hand, the length came from a member function
- call, you could change that mf to get the length from the
- representation. In other words, the class users wouldn't know
- that your implementation changed. That is the heart of
- encapsulation.
-
- As for performance, if that is a concern, an inline mf that
- returns the value of a dm is not slower than reading a public
- dm. Of course this raises implementation exposure issues for
- libraries which you may not want to incur (the subject of a
- recent thread).
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-